package com.ychs.service;

import com.ychs.dao.CollegeMapper;
import com.ychs.entity.College;
import com.ychs.util.Constants;
import com.ychs.util.DBUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.Map;

/**
 * copyright(c)2021 YYB.ALL rights Reserved
 * <p>
 * 描述:院系业务实现类
 * @author 原玉波
 * @version 1.0
 * @date 2021/5/21
 */
public class CollegeService implements IBaseService<College> {
    /**
     * 添加记录
     *
     * @param college 记录对象
     * @return 是否成功
     */
    @Override
    public boolean add(College college) {
        SqlSession session = DBUtil.getInstance().getSqlSession();
        CollegeMapper mapper = session.getMapper(CollegeMapper.class);
        int result = mapper.insert(college);
        session.commit();
        //session.close();
        DBUtil.close(session);

        return result== Constants.UPDATE_SUCCESS;
    }

    /**
     * 修改记录
     *
     * @param college 修改记录对象
     * @return 成功与否
     */
    @Override
    public boolean modify(College college) {
        SqlSession session = DBUtil.getInstance().getSqlSession();
        CollegeMapper mapper = session.getMapper(CollegeMapper.class);
        int result = mapper.update(college);

        session.commit();
        //session.close();
        DBUtil.close(session);

        return result== Constants.UPDATE_SUCCESS;
    }

    /**
     * 删除记录
     *
     * @param id 待删除记录的主键
     * @return 是否成功
     */
    @Override
    public boolean remove(int id) {
        SqlSession session = DBUtil.getInstance().getSqlSession();
        CollegeMapper mapper = session.getMapper(CollegeMapper.class);
        //删除之前判断它有没有被专业关联
        College college = mapper.selectById(id);
        //院系没有关联
        if(college.getMajors()==null || college.getMajors().isEmpty()){
            int result = mapper.delete(id);
            session.commit();
            //session.close();
            DBUtil.close(session);
            return result== Constants.UPDATE_SUCCESS;
        }else{
            return  false;
        }
    }

    /**
     * 查询单个对象
     *
     * @param id 查询对象的主键
     * @return 查询结果
     */
    @Override
    public College searchById(int id) {
        SqlSession session = DBUtil.getInstance().getSqlSession();
        CollegeMapper mapper = session.getMapper(CollegeMapper.class);
        College college = mapper.selectById(id);

        //session.close();
        DBUtil.close(session);

        return college;
    }

    /**
     * 查询多条记录
     *
     * @param conditions 查询条件
     * @return 查询结果
     */
    @Override
    public List<College> search(Map<String, Object> conditions) {
        SqlSession session = DBUtil.getInstance().getSqlSession();
        CollegeMapper mapper = session.getMapper(CollegeMapper.class);
        List<College> colleges = mapper.select(conditions);

        //session.close();
        DBUtil.close(session);

        return colleges;
    }

    /**
     * 查询总条数
     *
     * @param conditions 条件
     * @return
     */
    @Override
    public int searchTotalNumber(Map<String, Object> conditions) {
        SqlSession session = DBUtil.getInstance().getSqlSession();
        CollegeMapper mapper = session.getMapper(CollegeMapper.class);
        int result = mapper.selectTotalNumber(conditions);

        //session.close();
        DBUtil.close(session);

        return result;
    }
}
